import SideBar from '@/components/side-bar'
import { Separator } from '@/components/ui/separator'
import { auth } from '@/auth'
import { getAllTeams } from '@/models/team'
import { updateUser } from '@/models/user'

export default async function DashboardLayout({ children, params }: any) {
  const session: any = await auth()
  const teams = await getAllTeams(session?.user?.id)
  const currentTeam = params.teamSlug
  const isExist = teams.find(
    (team) => team.slug === decodeURIComponent(currentTeam)
  )
  if (!isExist) {
    throw Error('Team not found')
  } else {
    //那么如果当前和 user下的slug不一致需要更新
    if (isExist.slug !== session?.user?.currentTeamSlug) {
      await updateUser(session?.user?.id, {
        currentTeamSlug: isExist.slug,
      })
    }
  }

  return (
    <div className={'flex h-screen overflow-hidden'}>
      <SideBar />
      <Separator orientation={'vertical'} />
      <main className={'h-full w-full bg-[#fafafa]'}>{children}</main>
    </div>
  )
}
